package com.graduation.vaccine.controller.web;


import com.graduation.vaccine.service.CompanyService;
import com.graduation.vaccine.service.UserService;
import com.graduation.vaccine.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author:CaiShuangLian
 * @FileName:
 * @Date:Created in  2022/4/30 15:19
 * @Version:
 * @Description:TODO
 */
@RestController
@Api(tags = "web——部门模块")
@RequestMapping("/web/company")
public class WebCompanyController {

    @Autowired
    private UserService userService;
    @Autowired
    private CompanyService companyService;

    @ApiOperation("4.30:获取部门列表")
    @PostMapping("/all")
    @ResponseBody
    public RespBean getAll(@RequestBody String ticket, HttpServletRequest request, HttpServletResponse response){
        if(ticket!=null && !ticket.isEmpty()){
            //根据cookie获取用户信息
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(userVo!=null && userVo.getAction().equals(ActionEnum.SYSTEM_ADMINISTRATOR.getAction())){
                return companyService.getAll();
            }
            return RespBean.error(RespBeanEnum.NO_OPERATION_PERMISSION,"非系统管理员，无法查看，检查是否登陆过期");
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }

    @ApiOperation("4:30:部门下拉框")
    @ResponseBody
    @PostMapping("/dropDownBox")
    public RespBean companyDropDownBox(@RequestBody String ticket,HttpServletRequest request, HttpServletResponse response){
        if(ticket!=null && !ticket.isEmpty()){
            //根据cookie获取用户信息
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(userVo!=null && userVo.getAction().equals("system_administrator")){
                return companyService.companyDropDownBox();
            }
            return RespBean.error(RespBeanEnum.NO_OPERATION_PERMISSION,"非系统管理员，无法添加");
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }

    @ApiOperation("联合查询部门信息")
    @ResponseBody
    @PostMapping("/query/union")
    public RespBean unionQuery(@RequestBody CompanyUnionQueryVo companyUnionQueryVo,HttpServletRequest request, HttpServletResponse response){
        String ticket=companyUnionQueryVo.getTicket();
        if(ticket!=null && !ticket.isEmpty()){
            //根据cookie获取用户信息
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(userVo!=null && userVo.getAction().equals("system_administrator")){
                //name,description模糊查询
                return companyService.unionQuery(companyUnionQueryVo);
            }
            return RespBean.error(RespBeanEnum.NO_OPERATION_PERMISSION,"非系统管理员，无法添加");
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }

    @ApiOperation("4/30-04:修改部门信息（系统管理员修改所有，部门管理员修改本部门）")
    @ResponseBody
    @PostMapping("/update")
    public RespBean updateCompany(@RequestBody UpdateCompanyVo updateCompanyVo, HttpServletRequest request, HttpServletResponse response){
        String ticket=updateCompanyVo.getTicket();
        if(ticket!=null && !ticket.isEmpty()){
            //根据cookie获取用户信息
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(userVo!=null && (userVo.getAction().equals("system_administrator"))){
                //修改部门信息
                return companyService.updateCompany(updateCompanyVo);
            }else if(userVo.getAction().equals("organization_administrator")){
                //判断是否修改的是本部门
                if(userVo.getCompanyName().equals(updateCompanyVo.getName())){
                    return companyService.updateCompany(updateCompanyVo);
                }else {
                    return RespBean.error(RespBeanEnum.NO_OPERATION_PERMISSION,"不可修改其他部门的信息");
                }
            }
            return RespBean.error(RespBeanEnum.NO_OPERATION_PERMISSION,"非管理员，无法修改");
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }

    @ApiOperation("4/30——04：增加部门")
    @ResponseBody
    @PostMapping("/add")
    public RespBean addCompany(@RequestBody RegisterCompanyVo registerCompanyVo,HttpServletRequest request, HttpServletResponse response){
        String ticket=registerCompanyVo.getTicket();
        if(ticket!=null && !ticket.isEmpty()){
            //根据cookie获取用户信息
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(userVo!=null ){
                if(userVo.getAction().equals("system_administrator")){
                    return companyService.registerDepartment(registerCompanyVo);
                }
                return RespBean.error(RespBeanEnum.NO_OPERATION_PERMISSION);
            }
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }

    //获取本部门信息
    @ApiOperation("/获取本部门信息")
    @ResponseBody
    @PostMapping("/personal")
    public RespBean getOwnCompany(@RequestBody String ticket,HttpServletRequest request, HttpServletResponse response){
        if(ticket !=null && !ticket.isEmpty()){
            //根据cookie获取用户信息
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(null!=userVo){
                String companyName=userVo.getCompanyName();
                return companyService.getCompanyByName(companyName);
            }
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }


}
